package com.zhsq.zlc.controller.login;

import com.zhsq.zlc.entity.LogEntity;
import com.zhsq.zlc.entity.UserEntity;
import com.zhsq.zlc.service.authority.AuthorityService;
import com.zhsq.zlc.service.log.LogService;
import com.zhsq.zlc.service.user.UserService;
import com.zhsq.zlc.util.MenuUtil;
import com.zhsq.zlc.util.SessionInfo;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

	@Autowired
	private UserService userService;

	@Autowired
	private AuthorityService authorityService;

	@Autowired
	private LogService logService;

	private  static org.slf4j.Logger logger = LoggerFactory.getLogger(LoginController.class);
	/**
	 * 登录跳转
	 *
	 * @param model
	 * @return
	 */
	@GetMapping("/zhsq/login")
	public String loginGet(Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();
		if(session.getAttribute("sessionInfo")!=null){
			SessionInfo sessioninfo = (SessionInfo)session.getAttribute("sessionInfo");
			session.removeAttribute("sessionInfo");
			session.invalidate();
		}
		return "login";
	}

	/**
	 * 登录
	 *
	 * @param entity
	 * @param model
	 * @param httpSession
	 * @return
	 */
	@PostMapping("/zhsq/login")
	public String loginPost(UserEntity entity, Model model, HttpSession httpSession, HttpServletRequest request) throws Exception{
		UserEntity userRes = userService.findByNameAndPassword(entity);
		if (userRes != null) {
			SessionInfo sessioninfo = new SessionInfo();
			sessioninfo.setUser(userRes);
			sessioninfo.setUserAuthority(authorityService.getAuthorityTreeByRoleId(userRes.getRoleId()));
			sessioninfo.setUserAuthorityNavHtml(MenuUtil.getNavMenu(sessioninfo.getUserAuthority()));
			request.getSession().setAttribute("sessionInfo", sessioninfo);
			this.logService.insertLog(LogEntity.LOGIN_TYPE, "登陆成功"+entity.toString(), sessioninfo.getUser().getLoginName(), true);
			return "redirect:dashboard";
		} else {
			model.addAttribute("error", "账号或密码错误，请输入正确的账号密码！");
			this.logService.insertLog(LogEntity.LOGIN_TYPE, "登陆成功"+entity.toString(), entity.getLoginName(), true);
			return "login";
		}
	}

	/**
	 * 注销跳转
	 * @param model
	 * @param request
	 * @return
	 */
	@GetMapping("/zhsq/signout")
	public String signOutGet(Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();
		SessionInfo sessioninfo = (SessionInfo)session.getAttribute("sessionInfo");
		session.removeAttribute("sessionInfo");
		session.invalidate();
		return "login";
	}

	/**
	 * 获取Session
	 * @param model
	 * @param request
	 * @return
	 */
	@ResponseBody
	@PostMapping("/zhsq/getSessionInfo")
	public SessionInfo getSessionInfo(Model model,HttpServletRequest request) throws Exception{
		HttpSession session = request.getSession();
		SessionInfo sessioninfo = (SessionInfo)session.getAttribute("sessionInfo");
		return sessioninfo;
	}
	/**
	 * 仪表板页面
	 *
	 * @param model
	 * @return
	 */
	@GetMapping("/zhsq/dashboard")
	public String dashboard(Model model) {
		return "dashboard";
	}


	/**
	 * 登录
	 *
	 * @param entity
	 * @param model
	 * @param httpSession
	 * @return
	 */
	@GetMapping("/zhsq/index")
	public String index(UserEntity entity, Model model, HttpSession httpSession, HttpServletRequest request) throws Exception{
		entity.setLoginName("admin");
		entity.setPassword("123456");
		UserEntity userRes = userService.findByNameAndPassword(entity);
		if (userRes != null) {
			SessionInfo sessioninfo = new SessionInfo();
			sessioninfo.setUser(userRes);
			sessioninfo.setUserAuthority(authorityService.getAuthorityTreeByRoleId(userRes.getRoleId()));
			sessioninfo.setUserAuthorityNavHtml(MenuUtil.getNavMenu(sessioninfo.getUserAuthority()));
			request.getSession().setAttribute("sessionInfo", sessioninfo);
			this.logService.insertLog(LogEntity.LOGIN_TYPE, "登陆成功"+entity.toString(), sessioninfo.getUser().getLoginName(), true);
			return "redirect:dashboard";
		} else {
			model.addAttribute("error", "账号或密码错误，请输入正确的账号密码！");
			this.logService.insertLog(LogEntity.LOGIN_TYPE, "登陆成功"+entity.toString(), entity.getLoginName(), true);
			return "login";
		}
	}


}
